package com.orion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.dao.RoleDao;
import com.orion.dao.UserDao;
import com.orion.domain.Role;
import com.orion.domain.SysUser;
import com.orion.domain.User;
import com.orion.exception.CommonAuthenticationException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @date 2021/9/6
 */
@Service("authUserDetailService")
public class AuthUserDetailService implements UserDetailsService {

    @Resource(name = "userDao")
    private UserDao userDao;

    @Resource(name = "roleDao")
    private RoleDao roleDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDao.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccount, username));
        if (user == null) {
            throw new BadCredentialsException("用户[" + username + "] 没找到");
        }

        Integer status = user.getStatus();
        if (status == 0) {
            throw new DisabledException("status=0 无效用户");
        }

        if (status == 2) {
            throw new CredentialsExpiredException("status=2 密码过期");
        }

        if (status == 3) {
            throw new LockedException("status=3 账户已经锁定");
        }

        if (status == 4) {
            throw new CommonAuthenticationException("status=4 CommonAuthentication，哈哈哈");
        }

        List<Role> roles = roleDao.selectRoleByUserId(user.getId());
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        if (!roles.isEmpty()) {
            List<String> roleNames = roles.stream().map(Role::getRoleName).collect(Collectors.toList());
            for (String roleName : roleNames) {
                authorities.add(new SimpleGrantedAuthority(roleName));
            }
        }

        return new SysUser(username, user.getPassword(), authorities);
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("123456789");
        System.out.println(encode);
        System.out.println(bCryptPasswordEncoder.matches("123456789", encode));
    }
}
